home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12803 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  66 lines

  1. Newsgroups: comp.lang.c++
  2. Path: newshub.nosc.mil!news!sampson
  3. From: sampson@nosc.mil (Charles H. Sampson)
  4. Subject: Re: Specifying Data Layout
  5. Message-ID: <1996Mar21.171237.7459@nosc.mil>
  6. Sender: news@nosc.mil
  7. Organization: Computer Sciences Corporation
  8. References: <1996Mar14.173643.8651@nosc.mil> <VA.0000006c.00028969@fred>
  9. Date: Thu, 21 Mar 1996 17:12:37 GMT
  10.  
  11. In article <VA.0000006c.00028969@fred>,
  12. Frederic LACHASSE  <lachass@worldnet.fr> wrote:
  13. >In article <1996Mar14.173643.8651@nosc.mil>, sampson@cod.nosc.mil (mE) wrote:
  14. >> 
  15. >>      Is there some way of precisely specifying the memory layout of a
  16. >> struct?  In other words, can you say, "This struct is to occupy 3 words of
  17. >> memory, element foo is to occupy bits 5-12 of word 1, element fern is to
  18. >> occupy ..."?  If it can be done with a struct, can it also be done with a
  19. >> class (specifying locations for the data members only)?  In this case, can
  20. >> you also control the gizmo that is used to distinguish the various values
  21. >> of a class hierarchy, both where it is to be allocated and the values to be
  22. >> used to identify specific members of the hierarchy?
  23. >> 
  24. >The way to do it is:
  25. >
  26. >struct Packed
  27. >{
  28. >  unsigned short a:3; // 3 bits
  29. >  unsigned short b:5; // 5 more bits
  30. >  unsigned short c:8; // the remaining 8 bits for a 16 bits unsigned short
  31. >};
  32. >
  33. >The a, b, c member of the Packed struct will be stored in 2 bytes of memory.
  34. >
  35. >BTW, the same syntax works for classes.
  36. >
  37. >The exact layout of the bits is implementation dependant as:
  38. > - unsigned short isn't necessarily 16 bits long,
  39. > - the processor store most significant bytes first or last.
  40. >So read compiler documentation and experiment to find out which exact layout 
  41. >will work for you.
  42. >
  43. >One reason you've found nothing in the C++ FAQ is that it is a C syntax that 
  44. >C++ have inherited. As most C++ programmers learn C first, they already know 
  45. >this trick.
  46.  
  47.      I appreciate your response.  However, after reading it and two others
  48. that contained essentially the same information, it seems that the answer
  49. to my question is "No".  Finding out what your particular compiler does is
  50. not at all the same thing as specifying what any compiler is to do.
  51.  
  52.      It's not clear what "trick" you're referring to.  Bit-fields appear in
  53. Stroustrup, but basically he says that except for specifying the length of
  54. the component, everything is up for grabs.  I don't personally consider
  55. reading the compiler manual a trick, but based on my experience as some-
  56. thing like a consultant, apparently many people do. :-)
  57.  
  58.      By the way, none of the respondents have addressed the issue of con-
  59. trolling a classes "tag".  My guess is that it is impossible to either
  60. allocate it or specify its values.  (Neither can be done in Ada either.)
  61.  
  62.      (My apologies for leaving in so much quoted material, but I couldn't
  63. figure out how to cut much and have what was left make any sense.)
  64.  
  65.                                 Charlie
  66.